package com.study.heartBeat.listener;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.util.concurrent.ScheduledFuture;

/**
 * @ClassName: ServerHeartBeatListener
 * @Description:
 * @Date 2024/04/30 13:40
 * @Author songshaoqiu
 **/
public class ServerHeartBeatListener implements ChannelFutureListener {

    private Channel channel;

    private ScheduledFuture<?> scheduledFuture;

    public ServerHeartBeatListener(Channel channel, ScheduledFuture<?> scheduledFuture) {
        this.channel = channel;
        this.scheduledFuture = scheduledFuture;
    }

    public ScheduledFuture<?> getScheduledFuture() {
        return scheduledFuture;
    }

    public void setScheduledFuture(ScheduledFuture<?> scheduledFuture) {
        this.scheduledFuture = scheduledFuture;
    }


    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    @Override
    public void operationComplete(ChannelFuture channelFuture) throws Exception {
        if (channelFuture.isSuccess()){
            System.out.println("【服务端】-【监听器】:监听服务端发送心跳【正常】");
            return;
        }else{
            System.out.println("【服务端】-【监听器】:监听服务端发送心跳【失败】");
            channelFuture.channel().close();
            scheduledFuture.cancel(true);
        }
    }
}
